package hung.vnptthanhhoa.cookingbook;

import hung.vnptthanhhoa.adapter.ArrayAdapter_Food;
import hung.vnptthanhhoa.adapter.FoodList;
import hung.vnptthanhhoa.util.DBController;
import hung.vnptthanhhoa.util.Helper;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class Fragment_Food_List extends Fragment {
	private String url = "http://10.41.23.115/getMonAn.php";
	// private static String url = "http://192.168.1.17/getMonAn.php";
	private ListView dataList;
	private DBController controller;
	private ArrayList<FoodList> arrayListFood;
	private int limit = 10;
	private int start = 0;
	private int group_id;
	private ArrayAdapter_Food adapter;
	private boolean stack = false;

	public Fragment_Food_List(int group_id) {
		url = url + "?group_id=" + group_id;
		this.group_id = group_id;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_list_food,
				container, false);
		// get data from server
		controller = new DBController(getActivity());
		// new ListFood(rootView).execute();
		if (!stack)
			arrayListFood = controller.getArrayListByGroupId(group_id, start,
					limit);
		adapter = new ArrayAdapter_Food(getActivity(), arrayListFood);
		dataList = (ListView) rootView.findViewById(R.id.list_food);
		dataList.setAdapter(adapter);

		dataList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (Helper.networkState(getActivity())) {
					stack = true;
					ArrayAdapter_Food ad = (ArrayAdapter_Food) parent
							.getAdapter();
					FragmentTransaction transaction = getFragmentManager()
							.beginTransaction();
					transaction.setCustomAnimations(R.anim.exit, R.anim.exit,R.anim.exit,R.anim.enter_food_detail);
					Fragment_Food_Detail fg = new Fragment_Food_Detail(ad
							.getItem(position).getId_Sync(), ad.getItem(
							position).getImage());
					transaction.replace(R.id.root_frame, fg);
					transaction.addToBackStack("FOOD_LIST");
					transaction.commit();
				} else {
					Toast.makeText(getActivity(),
							"Hãy bật kết nối wifi hoặc 3G để sử dụng ứng dụng",
							Toast.LENGTH_LONG).show();
				}
			}
		});

		dataList.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {

				if (scrollState == SCROLL_STATE_IDLE) {
					if (dataList.getLastVisiblePosition() >= dataList
							.getCount() - 1) {
						if (dataList.getCount() == limit)
							new LoadMoreFood().execute();
					}
				}

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub

			}
		});

		return rootView;
	}

	public class LoadMoreFood extends
			AsyncTask<Void, Void, ArrayList<FoodList>> {
		private ProgressDialog pDialog;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(getActivity());
			pDialog.setMessage("Đang tải dữ liệu...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		@Override
		protected ArrayList<FoodList> doInBackground(Void... params) {
			start += limit;
			limit += 10;
			ArrayList<FoodList> listMonAn = controller.getArrayListByGroupId(
					group_id, start, limit);
			return listMonAn;
		}

		@Override
		protected void onPostExecute(ArrayList<FoodList> listMonAn) {
			pDialog.dismiss();
			arrayListFood.addAll(listMonAn);
			adapter.notifyDataSetChanged();
			// super.onPostExecute(listMonAn);
		}

	}
}
